package com.example.consumingrest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * 程序启动类
 */
@SpringBootApplication
public class ConsumingRestApplication {

	private static final Logger log = LoggerFactory.getLogger(ConsumingRestApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(ConsumingRestApplication.class, args);
	}

	/**
	 * 同步客户端执行HTTP请求  |  测试用
	 * @param builder : 可用于配置和创建RestTemplate的构建器
	 * @return : RestTemplate实例
	 */
	@Bean
	public RestTemplate restTemplate(RestTemplateBuilder builder) {
		return builder.build();
	}
	/**
	 * 在SpringApplication中运行
	 * @param restTemplate ：同步客户端执行HTTP请求
	 * @return ：响应的参数
	 * @throws Exception ：异常
	 */
	@Bean
	public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
		/**
		 * Lambda表达式
		 */
		return args -> {
			Quote quote = restTemplate.getForObject("https://quoters.apps.pcfone.io/api/random", Quote.class);
			log.info(quote.toString());
		};
	}
}
